<?php

class ConfigurationController extends Controller
{
	public function actionDetail()
	{
		//Bind list of users
		$criteria = new CDbCriteria();
		$criteria->condition = 'active=1';
		$users = User::model()->findAll($criteria);
		//Get Id
		$id = Yii::app()->request->getParam('id');
		$error = '';
		$msg = '';
		//Mode update
		if (is_numeric($id))
		{
			$model = Configuration::model()->findByPk($id);
			if ($model == null) 
			{
				$error = '<li>Configuration['.$id.'] not found. Please add new.</li>';
				$model = new Configuration;
			}
		}
		//Add New
		else
		{
			$model=new Configuration;
			$model->user = Yii::app()->user->loggedInUser->username;
			$model->installeddate = date('Y-m-d');
		}
		if(isset($_POST['Configuration']))
		{
			$model->attributes=$_POST['Configuration'];
			$fileName = CUploadedFile::getInstance($model, 'fileupload');
			$oldFileName = '';
			if ($fileName != '')
			{
				$oldFileName = $model->filename;
				$model->filename = $fileName;
			}
			//$error = $this->validate($model);
			if($error == '')
			{
				$isCreation = false;
				if ($model->id <= 0) 
				{
					$isCreation = true;
					$model->createddate = new CDbExpression('NOW()');
				}
				$model->modifieddate = new CDbExpression('NOW()');
				//Save success
				if ($model->save()) 
				{
					//Upload file
					if ($fileName != '')
					{
						//Check Store path, create if not exist
						$path = Yii::app()->params->appPath.DIRECTORY_SEPARATOR.Yii::app()->params->uploadPath;
						if (!file_exists($path))
						{
							mkdir($path);
						}
						$filePath = $path.DIRECTORY_SEPARATOR.$model->id.'_'.$model->filename;
						//Save file to path
						$model->filename->saveAs($filePath);
						//Delete old file
						if ($oldFileName != '')
						{
							$filePath = $path.DIRECTORY_SEPARATOR.$model->id.'_'.$oldFileName;
							if (file_exists($filePath))
								unlink($filePath);
						}
					}
					$msg = '<li>Configuration has been saved success.</li>';
					if ($isCreation)
					{
						Yii::app()->session['success_msg'] = $msg;
						$this->redirect(Yii::app()->createUrl('configuration/list'));
					}
				}
				else
				{
					foreach($model->getErrors() as $errs)
					{
						foreach($errs as $err)
						{
							$error	.= '<li>' . $err . '</li>';
						}	
					}
				}
			}
		}
		$this->render('detail',array('model'=>$model, 'users'=>$users, 'error'=>$error, 'msg'=>$msg));
	}
	
	public function actionDelete()
	{
		//Get Id
		$id = Yii::app()->request->getParam('id');
		$model = Configuration::model()->findByPk($id);
		$model->active = 0;
		$model->save();
	}
	public function actionIndex()
	{
		$this->actionList();
	}

	public function actionList()
	{
		$msg = '';
		if (isset(Yii::app()->session['success_msg']))
		{
			$msg = Yii::app()->session['success_msg'];
			unset(Yii::app()->session['success_msg']);
		}
		$criterias = new CDbCriteria(array(
			'order'=>'id DESC',
			'condition'=>'active=1'
		));
		$dataProvider = new CActiveDataProvider('Configuration', array('criteria'=>$criterias));
		$this->render('list', array('dataProvider'=>$dataProvider, 'msg'=>$msg));
	}

	/*
	** The method for validation Configuration.
	*/
	private function validate($model)
	{
		$error = '';
		if ($model->filename == '')
		{
			$error .= '<li>File name is required.</li>';	
		}
		if ($model->version == '')
		{
			$error .= '<li>Version is required.</li>';	
		}
		if ($model->installeddate != null)
		{
			$model->installeddate = date('Y-m-d', strtotime($model->installeddate));
		}
		return $error;	
	}
	
	public function accessRules()
	{
		return array(
			array(
				'deny',
				'actions'=>array('detail', 'delete', 'list', 'index'),
				'users'=>array('?')
			),
		);
	}
	// Uncomment the following methods and override them if needed
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'accessControl'
		);
	}
	/*
	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}